package com.pda.yuanjing.database;

import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.dao.InCodeDao;
import com.pda.yuanjing.dao.InStoreDao;
import com.pda.yuanjing.dao.OutCodeDao;
import com.pda.yuanjing.dao.OutStoreDao;
import com.pda.yuanjing.dao.RelationCodeDao;
import com.pda.yuanjing.dao.RelationRecordDao;
import java.util.ArrayList;
import java.util.List;

public class DataBaseControl {
    public static String TAG = "DataBaseControl";

    public static void clearData(){
        List<InStore> list1 = PDApp.daoSession.getInStoreDao().loadAll();
        PDApp.daoSession.getInStoreDao().deleteInTx(list1);
        List<InCode> list2 = PDApp.daoSession.getInCodeDao().loadAll();
        PDApp.daoSession.getInCodeDao().deleteInTx(list2);

        List<OutStore> list3 = PDApp.daoSession.getOutStoreDao().loadAll();
        PDApp.daoSession.getOutStoreDao().deleteInTx(list3);
        List<OutCode> list4 = PDApp.daoSession.getOutCodeDao().loadAll();
        PDApp.daoSession.getOutCodeDao().deleteInTx(list4);

        List<RelationRecord> list5 = PDApp.daoSession.getRelationRecordDao().loadAll();
        PDApp.daoSession.getRelationRecordDao().deleteInTx(list5);
        List<RelationCode> list6 = PDApp.daoSession.getRelationCodeDao().loadAll();
        PDApp.daoSession.getRelationCodeDao().deleteInTx(list6);
    }

    //region入库
    public static long addInBill(InStore entity) {
        long res = -1;
        try {
            res = PDApp.daoSession.getInStoreDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static void deleteInBill(String bill) {
        try {
            List<InStore> temps = PDApp.daoSession.getInStoreDao().queryBuilder().where(InStoreDao.Properties.BillID.eq(bill)).list();
            PDApp.daoSession.getInStoreDao().deleteInTx(temps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<InStore> getInBillAll() {
        List<InStore> list = new ArrayList<InStore>();
        try {
            list = PDApp.daoSession.getInStoreDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    public static void updateInBill(String billID,String productID ,int number)
    {
        try {
            InStore temp = PDApp.daoSession.getInStoreDao().queryBuilder().where(InStoreDao.Properties.BillID.eq(billID),InStoreDao.Properties.ProductID.eq(productID)).unique();
            temp.setSnum(number);
            PDApp.daoSession.getInStoreDao().update(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static long addInCode(InCode entity) {
        long res = -1;
        try {
            res = PDApp.daoSession.getInCodeDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static long deleteInCode(String billid,String productid,String code) {
        long res = -1;
        InCode temp = null;
        try {
            temp = PDApp.daoSession.getInCodeDao().queryBuilder().where(InCodeDao.Properties.BillID.eq(billid),InCodeDao.Properties.ProductID.eq(productid),InCodeDao.Properties.Code.eq(code)).unique();
            if(temp!=null){
                PDApp.daoSession.getInCodeDao().delete(temp);
                res = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return res;
        }
    }


    public static void deleteInCode(String billid) {
        try {
            List<InCode> list= PDApp.daoSession.getInCodeDao().queryBuilder().where(InCodeDao.Properties.BillID.eq(billid)).list();
            PDApp.daoSession.getInCodeDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return ;
        }
    }

    public static void updateInCode(String billid,boolean flag) {
        try {
            List<InCode> list = PDApp.daoSession.getInCodeDao().queryBuilder().where(InCodeDao.Properties.BillID.eq(billid)).list();
            for (int i = 0; i <list.size() ; i++) {
                list.get(i).setFlag(flag);
            }
            PDApp.daoSession.getInCodeDao().updateInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<InCode> getInCodes(String billid,String productid) {
        List<InCode> list = null;
        try {
            list = PDApp.daoSession.getInCodeDao().queryBuilder().where(InCodeDao.Properties.BillID.eq(billid),InCodeDao.Properties.ProductID.eq(productid)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    public static List<InCode> getInCodes(String billid) {
        List<InCode> list = new ArrayList<>();
        try {
            list = PDApp.daoSession.getInCodeDao().queryBuilder().where(InCodeDao.Properties.BillID.eq(billid)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    public static List<InCode> getInCodes(String billid,boolean flag) {
        List<InCode> list = new ArrayList<>();
        try {
            list = PDApp.daoSession.getInCodeDao().queryBuilder().where(InCodeDao.Properties.BillID.eq(billid),InCodeDao.Properties.Flag.eq(flag)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }
    //endregion

    //region出库
    public static long addOutBill(OutStore entity) {
        long res = -1;
        try {
            res = PDApp.daoSession.getOutStoreDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static void deleteOutBill(String id) {
        try {
            List<OutStore> temps = PDApp.daoSession.getOutStoreDao().queryBuilder().where(OutStoreDao.Properties.BillID.eq(id)).list();
            PDApp.daoSession.getOutStoreDao().deleteInTx(temps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<OutStore> getOutBillAll() {
        List<OutStore> list = new ArrayList<OutStore>();
        try {
            list = PDApp.daoSession.getOutStoreDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }


    public static void updateOutBill(String billID,String productID ,int number)
    {
        try {
            OutStore temp = PDApp.daoSession.getOutStoreDao().queryBuilder().where(OutStoreDao.Properties.BillID.eq(billID),OutStoreDao.Properties.ProductID.eq(productID)).unique();
            temp.setSnum(number);
            PDApp.daoSession.getOutStoreDao().update(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static long addOutCode(OutCode entity) {
        long res = -1;
        try {
            res = PDApp.daoSession.getOutCodeDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static long deleteOutCode(String billid,String productid,String code) {
        long res = -1;
        OutCode temp = null;
        try {
            temp = PDApp.daoSession.getOutCodeDao().queryBuilder().where(OutCodeDao.Properties.BillID.eq(billid),OutCodeDao.Properties.ProductID.eq(productid),OutCodeDao.Properties.Code.eq(code)).unique();
            if(temp!=null){
                PDApp.daoSession.getOutCodeDao().delete(temp);
                res = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return res;
        }
    }

    public static void deleteOutCode(String bill) {
        try {
            List<OutCode> list= PDApp.daoSession.getOutCodeDao().queryBuilder().where(OutCodeDao.Properties.BillID.eq(bill)).list();
            PDApp.daoSession.getOutCodeDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return ;
        }
    }

    public static void updateOutCode(String billid,boolean flag) {
        try {
            List<OutCode> list= PDApp.daoSession.getOutCodeDao().queryBuilder().where(OutCodeDao.Properties.BillID.eq(billid)).list();
            for (int i = 0; i <list.size() ; i++) {
                list.get(i).setFlag(flag);
            }
            PDApp.daoSession.getOutCodeDao().updateInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<OutCode> getOutCodes(String billid,String productid) {
        List<OutCode> list = null;
        try {
            list = PDApp.daoSession.getOutCodeDao().queryBuilder().where(OutCodeDao.Properties.BillID.eq(billid),OutCodeDao.Properties.ProductID.eq(productid)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    public static List<OutCode> getOutCodes(String billid) {
        List<OutCode> list = new ArrayList<>();
        try {
            list = PDApp.daoSession.getOutCodeDao().queryBuilder().where(OutCodeDao.Properties.BillID.eq(billid)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    public static List<OutCode> getOutCodes(String billid,boolean flag) {
        List<OutCode> list = new ArrayList<>();
        try {
            list = PDApp.daoSession.getOutCodeDao().queryBuilder().where(OutCodeDao.Properties.BillID.eq(billid),OutCodeDao.Properties.Flag.eq(flag)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }
    //endregion

    //region生产
    public static long addRelationBill(RelationRecord entity) {
        long res = -1;
        try {
            res = PDApp.daoSession.getRelationRecordDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static void deleteRelationBill(String bill) {
        try {
            List<RelationRecord> temps = PDApp.daoSession.getRelationRecordDao().queryBuilder().where(RelationRecordDao.Properties.BillID.eq(bill)).list();
            PDApp.daoSession.getRelationRecordDao().deleteInTx(temps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<RelationRecord> getRelationBillAll() {
        List<RelationRecord> list = new ArrayList<RelationRecord>();
        try {
            list = PDApp.daoSession.getRelationRecordDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    public static void updateRelationBill(String billID,String productID ,int number)
    {
        try {
            RelationRecord temp = PDApp.daoSession.getRelationRecordDao().queryBuilder().where(RelationRecordDao.Properties.BillID.eq(billID),RelationRecordDao.Properties.ProductID.eq(productID)).unique();
            temp.setSnum(number);
            PDApp.daoSession.getRelationRecordDao().update(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static long addRelationCode(RelationCode entity) {
        long res = -1;
        try {
            res = PDApp.daoSession.getRelationCodeDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    public static int  deleteCaseCode(String bill,String code) {
        List<RelationCode> list = null;
        int res = -1;
        try {
            list = PDApp.daoSession.getRelationCodeDao().queryBuilder().where(RelationCodeDao.Properties.BillID.eq(bill),RelationCodeDao.Properties.CaseCode.eq(code)).list();
            if(list!=null && list.size()>0){
                PDApp.daoSession.getRelationCodeDao().deleteInTx(list);
                res = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return res;
        }
    }

    public static void deleteCode(String bill) {
        try {
            PDApp.daoSession.getRelationCodeDao().deleteInTx(PDApp.daoSession.getRelationCodeDao().queryBuilder().where(RelationCodeDao.Properties.BillID.eq(bill)).list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updateRelationCode(String billid,boolean flag) {
        try {
            List<RelationCode> list= PDApp.daoSession.getRelationCodeDao().queryBuilder().where(OutCodeDao.Properties.BillID.eq(billid)).list();
            for (int i = 0; i <list.size() ; i++) {
                list.get(i).setFlag(flag);
            }
            PDApp.daoSession.getRelationCodeDao().updateInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int uniqueeBoxCode(String bill,String boxCode) {
        int res = 0;
        try {
          RelationCode relationCode = PDApp.daoSession.getRelationCodeDao().queryBuilder().where(RelationCodeDao.Properties.BillID.eq(bill),RelationCodeDao.Properties.BoxCode.eq(boxCode)).unique();
          if(relationCode!=null){
              res = -1;
          }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return res;
        }
    }

    public static int uniqueeCaseCode(String bill,String caseCode) {
        int res = 0;
        try {
            List<RelationCode> list = PDApp.daoSession.getRelationCodeDao().queryBuilder().where(RelationCodeDao.Properties.BillID.eq(bill),RelationCodeDao.Properties.CaseCode.eq(caseCode)).list();
            if(list!=null && list.size()>0){
                res = -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return res;
        }
    }

    public static List<RelationCode> getRelationCode(String bill) {
        List<RelationCode> list = null;
        try {
             list = PDApp.daoSession.getRelationCodeDao().queryBuilder().where(RelationCodeDao.Properties.BillID.eq(bill)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    public static List<RelationCode> getRelationCode(String bill,boolean flag) {
        List<RelationCode> list = null;
        try {
            list = PDApp.daoSession.getRelationCodeDao().queryBuilder().where(RelationCodeDao.Properties.BillID.eq(bill),RelationCodeDao.Properties.Flag.eq(flag)).list();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    //endregion
}


